package atomic;

import java.util.concurrent.atomic.LongAccumulator;
import java.util.concurrent.atomic.LongAdder;

/**
 * @author fei
 * @date 2023/1/21 10:31
 * @description
 */
public class LongAdderAPIDemo {

    public static void main(String[] args) {
        LongAdder longAdder = new LongAdder();

        longAdder.increment();
        longAdder.increment();
        longAdder.increment();

        System.out.println(longAdder.sum());

        // 自定义函数 (x, y) -> x + y
        LongAccumulator longAccumulator = new LongAccumulator((x, y) -> x + y, 0);

        // 使用自定义函数操作
        longAccumulator.accumulate(1);
        longAccumulator.accumulate(3);

        System.out.println(longAccumulator.get());
    }

}
